﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using System.Web.UI;
using System.Web.UI.WebControls;
using System.Resources;
using System.Collections;
using System.Data.SqlClient;
using System.Configuration;
using System.Data;
using UCNKompetenceBors.Database;
using UCNKompetenceBors.Model;

namespace UCNKompetenceBors.Admin
{
    public partial class adminInformation : System.Web.UI.Page
    {
        protected void Page_Load(object sender, EventArgs e)
        {
            if (!Page.IsPostBack)
            {
                ckeditorInformationText.Visible = false;
                ckeditorInformationText.Width = 700;
                ddlInformationControls.Visible = false;
                this.Form.DefaultButton = btnUpdateInformation.UniqueID;

                BindSubMenu();
                BindPageDropdown();
            }

            #region Globalization
            string culture = String.Empty;

            if (HttpContext.Current.Session["MyCulture"] != null)
                culture = HttpContext.Current.Session["MyCulture"].ToString();

            if (!String.IsNullOrEmpty(culture))
                GeneralTools.SetCulture(culture);

            LocalizeControls();
            #endregion            
        }

        /// <summary>
        /// Binds the sub menu links.
        /// </summary>
        private void BindSubMenu()
        {
            hyLinkAdminDashboard.NavigateUrl = "~/Admin/admin.aspx";
            hyLinkUserHandling.NavigateUrl = "~/Admin/adminUsers.aspx";
            hyLinkAdminExchange.NavigateUrl = "~/Admin/adminExchange.aspx";
            hyLinkNewsHandling.NavigateUrl = "~/Admin/adminNews.aspx";
            hyLinkEditInformation.NavigateUrl = "~/Admin/adminInformation.aspx";
            hyLinkContactPerson.NavigateUrl = "~/Admin/adminContactPerson.aspx";
            hylinkDepartment.NavigateUrl = "~/Admin/adminDepartment.aspx";
            hyLinkResetUsers.NavigateUrl = "~/Admin/adminUsersReset.aspx";
        }

        /// <summary>
        /// Binds the page dropdown list with pages.
        /// </summary>
        private void BindPageDropdown()
        {
            ddlInformationPage.Items.Add(new ListItem("Vælg Side", "Vælg Side"));

            List<string> pageIds = dbAdmin.GetInformationPageIds();

            foreach (string pageId in pageIds)
            {
                ListItem li = new ListItem();

                li.Text = pageId;
                li.Value = pageId;

                ddlInformationPage.Items.Add(li);
            }
        }

        /// <summary>
        /// Binds the control dropdown list with controls, based on the pageId parameter.
        /// </summary>
        /// <param name="pageId">Specifies the page to be used.</param>
        private void BindControlDropdown(string pageId)
        {
            ddlInformationControls.Items.Clear();

            ddlInformationControls.Items.Add(new ListItem("Vælg Tekst", "Vælg Tekst"));

            List<Information> controls = dbAdmin.GetInformationControls(pageId);

            foreach (Information inf in controls)
            {
                ListItem li = new ListItem();

                string culture = String.Empty;

                if (HttpContext.Current.Session["MyCulture"] != null)
                    culture = HttpContext.Current.Session["MyCulture"].ToString();

                if(culture.Equals("en-GB"))
                    li.Text = inf.InformationLocalizedDescription;
                else
                    li.Text = inf.InformationDescription;

                li.Value = inf.InformationControlID;

                ddlInformationControls.Items.Add(li);
            }
        }

        /// <summary>
        /// Updates the information, based on the input parameters.
        /// </summary>
        /// <param name="informationText">Specifies the text to be used.</param>
        /// <param name="controlId">Specifies the ID of the control to be used.</param>
        /// <param name="pageId">Specifies the ID of the page to be used.</param>
        private void UpdateInformation(string informationText, string controlId, string pageId)
        {
            dbAdmin.UpdateInformation(informationText, controlId, pageId);
        }

        protected void btnUpdateInformation_Click(object sender, EventArgs e)
        {
            
            UpdateInformation(ckeditorInformationText.Text, ddlInformationControls.SelectedValue, ddlInformationPage.SelectedValue);
        }

        //Event fired when the selected index of the page dropdown list has changed
        protected void ddlInformationPage_SelectedIndexChanged(object sender, EventArgs e)
        {
            ckeditorInformationText.Visible = false;
            ddlInformationControls.Visible = true;

            BindControlDropdown(ddlInformationPage.SelectedValue);

            ddlInformationControls.Items[0].Text = GetLocalResourceObject("ddlInformationControlsText").ToString();
        }

        /// <summary>
        /// Binds the text field, based on the pageId and controlId parameters.
        /// </summary>
        /// <param name="pageId">Specifies the ID of the page to be used.</param>
        /// <param name="controlId">Specifies the ID of the control to be used.</param>
        /// <returns>Returns a string containing the resulting text.</returns>
        private void BindTextBox(string pageId, string controlId)
        {
            ckeditorInformationText.Visible = true;
            ckeditorInformationText.Text = dbAdmin.GetInformationText(pageId, controlId);
        }

        //Event fired when the selected index of the control dropdown list has changed
        protected void ddlInformationControls_SelectedIndexChanged(object sender, EventArgs e)
        {
            BindTextBox(ddlInformationPage.SelectedValue, ddlInformationControls.SelectedValue);
        }

        /// <summary>
        /// Localizes the controls of the site, based on the current UI Culture.
        /// </summary>
        private void LocalizeControls()
        {
            hyLinkAdminDashboard.Text = GetLocalResourceObject("hyLinkAdminDashboardText").ToString();
            hyLinkUserHandling.Text = GetLocalResourceObject("hyLinkUserHandlingText").ToString();
            hyLinkAdminExchange.Text = GetLocalResourceObject("hyLinkAdminExchangeText").ToString();
            hyLinkNewsHandling.Text = GetLocalResourceObject("hyLinkNewsHandlingText").ToString();
            hyLinkEditInformation.Text = GetLocalResourceObject("hyLinkEditInformationText").ToString();
            hyLinkResetUsers.Text = GetLocalResourceObject("hyLinkResetUsersText").ToString();
            hyLinkContactPerson.Text = GetLocalResourceObject("hyLinkContactPersonText").ToString();
            hylinkDepartment.Text = GetLocalResourceObject("hylinkDepartmentText").ToString();

            lblAdminContentHeader.Text = GetLocalResourceObject("lblAdminContentHeaderText").ToString();

            ddlInformationPage.Items[0].Text = GetLocalResourceObject("ddlInformationPageText").ToString();
            

            btnUpdateInformation.Text = GetLocalResourceObject("btnUpdateInformationText").ToString();

            string culture = String.Empty;

            if (HttpContext.Current.Session["MyCulture"] != null)
                culture = HttpContext.Current.Session["MyCulture"].ToString();

            if (culture.Equals("en-GB"))
                ckeditorInformationText.Language = "en";
            else
                ckeditorInformationText.Language = "da";
        }
    }
}